package com.pt.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.pt.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pt.common.annotation.Log;
import com.pt.common.core.controller.BaseController;
import com.pt.common.core.domain.AjaxResult;
import com.pt.common.enums.BusinessType;
import com.pt.manage.domain.Trainingcourse;
import com.pt.manage.service.ITrainingcourseService;
import com.pt.common.utils.poi.ExcelUtil;
import com.pt.common.core.page.TableDataInfo;

/**
 * 课程管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/manage/trainingcourse")
public class TrainingcourseController extends BaseController
{
    @Autowired
    private ITrainingcourseService trainingcourseService;


    /**
     * 查询课程管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Trainingcourse trainingcourse)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> params = new HashMap<>();
        params.put("teacherId", userId); // 传入具体的 teacherid
        params.put("trainingCourse", trainingcourse); // 传入 trainingcourse 对象
        List<Trainingcourse> list = trainingcourseService.selectTrainingcourseList(params);
        return getDataTable(list);
    }
    @GetMapping("/Studentlist")
    public TableDataInfo Studentlist(Trainingcourse trainingcourse)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> params = new HashMap<>();
        params.put("StudentId", userId); // 传入具体的 teacherid
//      params.put("trainingcourse", trainingcourse); // 传入 trainingcourse 对象
        List<Trainingcourse> list = trainingcourseService.selectStudentcourseList(params);
        return getDataTable(list);
    }
//    /**
//     * 导出课程管理列表
//     */
//    @PreAuthorize("@ss.hasPermi('manage:trainingcourse:export')")
//    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Trainingcourse trainingcourse)
//    {
//        List<Trainingcourse> list = trainingcourseService.selectTrainingcourseList(trainingcourse);
//        ExcelUtil<Trainingcourse> util = new ExcelUtil<Trainingcourse>(Trainingcourse.class);
//        util.exportExcel(response, list, "课程管理数据");
//    }

    /**
     * 获取课程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:trainingcourse:query')")
    @GetMapping(value = "/{CourseID}")
    public AjaxResult getInfo(@PathVariable("CourseID") Long CourseID)
    {

        return success(trainingcourseService.selectTrainingcourseByCourseID(CourseID));
    }

    /**
     * 新增课程管理
     */

    @PreAuthorize("@ss.hasPermi('manage:trainingcourse:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Trainingcourse trainingcourse)
    {

        return toAjax(trainingcourseService.insertTrainingcourse(trainingcourse));
    }
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping(value = "Student")
    public AjaxResult addStudent(@RequestBody Trainingcourse trainingcourse)
    {
        Map<String, Object> params = new HashMap<>();
        Long userId = SecurityUtils.getUserId();
        params.put("studentId",userId);
        params.put("courseCode",trainingcourse.getCourseCode());
        try {
            int result = trainingcourseService.insertStudentCourse(params);
            if (result > 0) {
                return toAjax(true); // 插入成功
            } else {
                return AjaxResult.error("插入失败，未找到匹配的课程或学生信息");// 自定义失败消息
            }
        } catch (Exception e) {
            // 捕获异常并返回自定义错误消息
            return AjaxResult.error("操作失败：" + e.getMessage());
        }
    }
    /**
     * 修改课程管理
     */
    @PreAuthorize("@ss.hasPermi('manage:trainingcourse:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Trainingcourse trainingcourse)
    {
        return toAjax(trainingcourseService.updateTrainingcourse(trainingcourse));
    }

    /**
     * 删除课程管理
     */
    @PreAuthorize("@ss.hasPermi('manage:trainingcourse:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{CourseIDs}")
    public AjaxResult remove(@PathVariable Long[] CourseIDs)
    {
        return toAjax(trainingcourseService.deleteTrainingcourseByCourseIDs(CourseIDs));
    }
}
